package com.lizardmind.haofudoctor.ui.studio.sickEducation

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.adapter.BaseAdapter
import com.linxiao.framework.util.getDataBinding
import com.linxiao.framework.util.toasts
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivitySickEducationBinding
import com.lizardmind.haofudoctor.databinding.ItemMyEduBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.MySickEducationBean
import com.lizardmind.haofudoctor.other.Request
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_sick_education.*
import kotlinx.android.synthetic.main.include_title_right_text.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivity

class SickEducationActivity : BaseActivity() {

    private val list= arrayListOf<MySickEducationBean>()
    private lateinit var headView:View
    val adapter by lazy { BaseAdapter<MySickEducationBean,ItemMyEduBinding>(R.layout.item_my_edu,list) }

    val binding by lazy { getDataBinding<ActivitySickEducationBinding>(R.layout.activity_sick_education) }
    override fun onCreatRootView(): View? {
        headView=LayoutInflater.from(this).inflate(R.layout.head_sick_edu,null)
        return binding.root
    }

    override fun onInitView(savedInstanceState: Bundle?) {

        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text="患者教育"

        adapter.headerView=headView
        rvSickEducation.layoutManager=LinearLayoutManager(this)
        rvSickEducation.adapter=adapter

        adapter.onBind { itemBingding, position, data ->
            toasts(list[position].title)
        }

        fLayoutWrightArticle.onClick { toasts("写文章") }
        rLayoutSystemEdu.onClick { startActivity<SystemSickEduActivity>("uid" to (intent.getStringExtra("uid")?:"")) }

        refreshLayout.startRefresh()
        refreshLayout.setEnableLoadmore(false)
        refreshLayout.setOnRefreshListener(object :RefreshListenerAdapter(){
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                list.clear()
                getMyEdu()
            }
        })

    }

    //获取我的患教库
    private fun getMyEdu() {
        observe {
            postApi<Request<ArrayList<MySickEducationBean>>>(
                    hashMapOf("action" to "document",
                            "token" to DoctorInfo.token),
                    Consumer {
                        it.dispose {
                            list.addAll(it)
                        }
                    }, Action { refreshLayout.finishRefreshing() })
        }
    }

}
